# API 测试脚本
Write-Host "================================" -ForegroundColor Cyan
Write-Host "开始测试后端 API" -ForegroundColor Cyan
Write-Host "================================`n" -ForegroundColor Cyan

# 测试1: 检查后端是否运行
Write-Host "1. 检查后端服务..." -ForegroundColor Yellow
$port8081 = netstat -ano | findstr ":8081.*LISTENING"
if ($port8081) {
    Write-Host "   ✓ 后端服务运行中 (端口 8081)" -ForegroundColor Green
} else {
    Write-Host "   ✗ 后端服务未运行！" -ForegroundColor Red
    exit
}

# 测试2: 测试根路径
Write-Host "`n2. 测试根路径..." -ForegroundColor Yellow
try {
    $response = Invoke-WebRequest -Uri "http://localhost:8081/api" -Method Get -ErrorAction Stop
    Write-Host "   ✓ /api 路径可访问" -ForegroundColor Green
} catch {
    Write-Host "   ✗ /api 路径错误: $($_.Exception.Message)" -ForegroundColor Red
}

# 测试3: 测试 item/list API (不带参数)
Write-Host "`n3. 测试 /api/item/list (空参数)..." -ForegroundColor Yellow
try {
    $body = @{} | ConvertTo-Json
    $response = Invoke-RestMethod -Uri "http://localhost:8081/api/item/list" -Method Post -Body $body -ContentType "application/json" -ErrorAction Stop
    Write-Host "   ✓ 请求成功" -ForegroundColor Green
    Write-Host "   - code: $($response.code)" -ForegroundColor Cyan
    Write-Host "   - msg: $($response.msg)" -ForegroundColor Cyan
    if ($response.data) {
        Write-Host "   - total: $($response.data.total)" -ForegroundColor Cyan
    }
} catch {
    $statusCode = $_.Exception.Response.StatusCode.value__
    Write-Host "   ✗ 请求失败 (状态码: $statusCode)" -ForegroundColor Red
    Write-Host "   - 错误: $($_.Exception.Message)" -ForegroundColor Red
}

# 测试4: 测试 item/list API (带参数)
Write-Host "`n4. 测试 /api/item/list (带参数 current=1, size=10)..." -ForegroundColor Yellow
try {
    $body = @{
        current = 1
        size = 10
    } | ConvertTo-Json
    Write-Host "   发送数据: $body" -ForegroundColor Gray
    $response = Invoke-RestMethod -Uri "http://localhost:8081/api/item/list" -Method Post -Body $body -ContentType "application/json" -ErrorAction Stop
    Write-Host "   ✓ 请求成功" -ForegroundColor Green
    Write-Host "   - code: $($response.code)" -ForegroundColor Cyan
    Write-Host "   - msg: $($response.msg)" -ForegroundColor Cyan
    if ($response.data) {
        Write-Host "   - total: $($response.data.total)" -ForegroundColor Cyan
        Write-Host "   - size: $($response.data.size)" -ForegroundColor Cyan
        Write-Host "   - current: $($response.data.current)" -ForegroundColor Cyan
    }
} catch {
    $statusCode = $_.Exception.Response.StatusCode.value__
    Write-Host "   ✗ 请求失败 (状态码: $statusCode)" -ForegroundColor Red
    Write-Host "   - 错误: $($_.Exception.Message)" -ForegroundColor Red
    
    # 尝试读取响应内容
    try {
        $reader = New-Object System.IO.StreamReader($_.Exception.Response.GetResponseStream())
        $responseBody = $reader.ReadToEnd()
        Write-Host "   - 响应内容: $responseBody" -ForegroundColor Red
    } catch {}
}

# 测试5: 测试 item/list API (带type参数)
Write-Host "`n5. 测试 /api/item/list (查询失物 type=0)..." -ForegroundColor Yellow
try {
    $body = @{
        current = 1
        size = 10
        type = 0
    } | ConvertTo-Json
    $response = Invoke-RestMethod -Uri "http://localhost:8081/api/item/list" -Method Post -Body $body -ContentType "application/json" -ErrorAction Stop
    Write-Host "   ✓ 请求成功" -ForegroundColor Green
    Write-Host "   - total: $($response.data.total)" -ForegroundColor Cyan
} catch {
    $statusCode = $_.Exception.Response.StatusCode.value__
    Write-Host "   ✗ 请求失败 (状态码: $statusCode)" -ForegroundColor Red
}

Write-Host "`n================================" -ForegroundColor Cyan
Write-Host "测试完成" -ForegroundColor Cyan
Write-Host "================================" -ForegroundColor Cyan

